package com.topica.network;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.PropertyInfo;

import android.content.Context;

import com.topica.database.DataBaseHelper;
import com.topica.database.task.TopicaDBTask;
import com.topica.object.QuizLogItem;

public class SendMultiMark extends TopicaDBTask {
	private ArrayList<QuizLogItem> data;
	private static final String METHOD_INFO_COURSE = "updatemark";

	public SendMultiMark(Context context) {
		super(context);
	}

	@Override
	protected Integer doInBackground(Void... params) {
		boolean rs;
		DataBaseHelper myDbHelper = new DataBaseHelper(
				context.getApplicationContext());
		data = myDbHelper.getQuizLog();
		if(data.isEmpty()){
			System.out.println("Khong co diem de gui");
		}
		for (QuizLogItem item : data) {
			rs = sendMark(item);
			if (rs) {
				myDbHelper.updateQuizLog(item.getId(), item.getQuizId(),
						item.getUser_id());
				System.out.println("Send diem thanh cong");
			}
		}
		myDbHelper.close();
		return OK;
	}

	private boolean sendMark(QuizLogItem logItem) {
		PropertyInfo pi = new PropertyInfo();
		pi.setName("value");
		String valueSend = "values(" + logItem.toString() + ")";
		pi.setValue(valueSend);
		List<PropertyInfo> pList = new ArrayList<PropertyInfo>();
		pList.add(pi);
		SoapCaller caller = new SoapCaller();
		int value = caller.updateMark(METHOD_INFO_COURSE, pList);
		if (value == 1)
			return true;
		else
			return false;
	}

}
